package com.wangsd.web.coupon.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Coupon;
import com.wangsd.web.coupon.entity.CouponStore;
import com.wangsd.web.coupon.entity.Product;
import com.wangsd.web.coupon.entity.Store;
import com.wangsd.web.coupon.service.CouponStoreService;
import com.wangsd.web.coupon.service.ProductService;
import com.wangsd.web.coupon.service.StoreService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-10
 */
@Api(value = "store", tags = "门店管理")
@RestController
@RequestMapping("/coupon/store")
public class StoreController extends MyController {

    @Autowired
    StoreService storeService;

    @Autowired
    ProductService productService;

    @Autowired
    CouponStoreService couponStoreService;

    @ApiOperation(value = "查询门店列表", notes = "查询门店列表", httpMethod = "POST")
    @RequestMapping(value = "/selectList/{pageNum}/{pageSize}", method = RequestMethod.POST)
    public Result<IPage<Store>> selectList(@ApiParam(value = "当前页", required = true) @PathVariable("pageNum") Integer pageNum,
                                           @ApiParam(value = "每页大小", required = true) @PathVariable("pageSize") Integer pageSize,
                                           @RequestBody @ApiParam(name = "params", value = "传入json格式") Store params) {
        SysUser loginUser = getUserInfo();
        IPage<Store> resultIPage = storeService.selectPage(pageNum, pageSize, params, loginUser.getCode());
        return Result.success(resultIPage);
    }


    @ApiOperation(value = "新增或修改门店", notes = "新增或修改门店", httpMethod = "POST", response = Result.class)
    @RequestMapping(value = "addOrUpdate", method = RequestMethod.POST)
    public Result addOrUpdate(Store params) {

        if (params.getMerchantId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "商户" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(params.getName())) {
            return new Result(PRARM_NOT_NULL.getCode(), "名称" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(params.getAddress())) {
            return new Result(PRARM_NOT_NULL.getCode(), "地址" + PRARM_NOT_NULL.getMessage());
        }

        if (StringUtils.isBlank(params.getOutShopId())) {
            return new Result(PRARM_NOT_NULL.getCode(), "门店id" + PRARM_NOT_NULL.getMessage());
        }

        Store repeatOutShopId = storeService.selectOutShopIdRepeat(params.getOutShopId(), params.getId());
        if (repeatOutShopId != null) {
            return new Result(PRARM_NOT_NULL.getCode(), "门店id重复");
        }

        storeService.addOrUpdate(params);
        return Result.success();
    }

    @ApiOperation(value = "删除门店信息", notes = "删除门店信息", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public Result delete(@ApiParam(value = "用户id", required = true) @PathVariable("id") Long id) {
        List<Product> productList = productService.list(new QueryWrapper<Product>().eq("store_id", id));
        if (productList != null && productList.size() > 0) {
            return Result.fail("请先删除当前门店下的商品");
        }
        List<CouponStore> coupons = couponStoreService.list(new QueryWrapper<CouponStore>().eq("store_id", id));
        if (coupons != null && coupons.size() > 0) {
            return Result.fail("请先删除当前门店下的优惠券");
        }
        storeService.removeById(id);
        return Result.success();
    }

    @ApiOperation(value = "查询所有门店", notes = "查询所有门店", httpMethod = "GET", response = Result.class)
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public Result selectAll() {
        SysUser loginUser = getUserInfo();
        List<Map> list = storeService.selectAllByUserCode(loginUser.getCode());
        return Result.success(list);
    }

    @ApiOperation(value = "通过id查询门店", notes = "通过id查询门店", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectById/{id}")
    public Result<Store> selectById(@ApiParam(value = "服务商id", required = true) @PathVariable Long id) {
        Store result = storeService.getById(id);
        return Result.success(result);
    }

    @ApiOperation(value = "查询层级关系树形结构", notes = "查询层级关系树形结构", httpMethod = "GET")
    @RequestMapping(value = "/selectTree", method = RequestMethod.GET)
    public Result<IPage<Store>> selectTree(@ApiParam(value = "限制那些层级不能点击", required = true) @RequestParam String ctrlType,
                                           @ApiParam(value = "限制那些层级不能点击", required = true) @RequestParam String displayType

    ) {
        SysUser loginUser = getUserInfo();
        JSONArray result = storeService.selectTree(ctrlType, displayType, loginUser.getType(), loginUser.getCode());
        return Result.success(result);
    }
}

